package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.DiscussDao;
import cn.sumpu.app.guoguojie.domain.Discuss;
import cn.sumpu.app.guoguojie.domain.DiscussTag;
import cn.sumpu.app.guoguojie.domain.DiscussTagDis;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DiscussDaoImpl extends SqlSessionDaoSupport implements DiscussDao {

    /**
     * 新增帖子
     * @param discuss
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#insertDiscuss(cn.sumpu.app.guoguojie.domain.Discuss)
     */
    @Override
    public void insertDiscuss(Discuss discuss) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DiscussMapper.insertDiscuss", discuss);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertDiscuss database failure.", e);
            }
            throw new AppBusinessException("insertDiscuss database failure.", e);
        }

    }

    /**
     * 更新帖子
     * @param discuss
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#updateDiscuss(cn.sumpu.app.guoguojie.domain.Discuss)
     */
    @Override
    public void updateDiscuss(Discuss discuss) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DiscussMapper.updateDiscuss", discuss);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateDiscuss database failure.", e);
            }
            throw new AppBusinessException("updateDiscuss database failure.", e);
        }

    }

    /**
     * 根据条件查询帖子总数
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#getDiscussCount(java.util.Map)
     */
    @Override
    public int getDiscussCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getDiscussCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDiscussCount database failure.", e);
            }
            throw new AppBusinessException("getDiscussCount database failure.", e);
        }
    }

    /**
     * 根据条件查询帖子
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#getDiscussList(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Discuss> getDiscussList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getDiscussList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDiscussList database failure.", e);
            }
            throw new AppBusinessException("getDiscussList database failure.", e);
        }
    }
    /**
     * 文稿详情
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#getDiscuss(java.util.Map)
     */
    @Override
    public Discuss getDiscuss(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Discuss) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getDiscuss", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDiscuss database failure.", e);
            }
            throw new AppBusinessException("getDiscuss database failure.", e);
        }
    }
    /**
     * 增加文稿标签
     * @param discussTag
     * @throws AppBusinessException
     */
    @Override
    public void insertDiscussTag(DiscussTag discussTag) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DiscussMapper.insertDiscussTag", discussTag);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertDiscussTag database failure.", e);
            }
            throw new AppBusinessException("insertDiscussTag database failure.", e);
        }

    }
    /**
     * 删除标签
     */
    @Override
    public void deleteDiscussTagById(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DiscussMapper.deleteDiscussTagById", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteDiscussTagById database failure.", e);
            }
            throw new AppBusinessException("deleteDiscussTagById database failure.", e);
        }
    }
    /**
     * 删除话题
     */
    @Override
    public void deleteDis(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DiscussMapper.deleteDis", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteDis database failure.", e);
            }
            throw new AppBusinessException("deleteDis database failure.", e);
        }
    }
    /**
     * 
     */
    
    @Override
    public void deleteDTSById(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DiscussMapper.deleteDTSById", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteDTSById database failure.", e);
            }
            throw new AppBusinessException("deleteDTSById database failure.", e);
        }
    }
    /**
     * 给文稿打标签
     * @param discussTagDis
     * @throws AppBusinessException
     */
    @Override
    public void insertDiscussTagDis(DiscussTagDis discussTagDis) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DiscussMapper.insertDiscussTagDis", discussTagDis);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertDiscussTagDis database failure.", e);
            }
            throw new AppBusinessException("insertDiscussTagDis database failure.", e);
        }

    }
    /**
     * 根据条件查询频道内的标签
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#getTagByCid(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<DiscussTag> getTagByCid(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getTagByCid", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getTagByCid database failure.", e);
            }
            throw new AppBusinessException("getTagByCid database failure.", e);
        }
    }
    /**
     * 根据条件查询频道内的标签
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.DiscussDao#getTagByCid(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<DiscussTagDis> getDTSList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getDTSList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDTSList database failure.", e);
            }
            throw new AppBusinessException("getDTSList database failure.", e);
        }
    }
    //查询话题
    @SuppressWarnings("unchecked")
    @Override
    public List<Discuss> getDiscussBruce(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DiscussMapper.getDiscussBruce", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDiscussBruce database failure.", e);
            }
            throw new AppBusinessException("getDiscussBruce database failure.", e);
        }
    }
    //修改量值
    @Override
    public void updateDiscussAdd(Discuss discuss) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.DiscussMapper.updateDiscussAdd", discuss);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateDiscussAdd database failure.", e);
            }
            throw new AppBusinessException("updateDiscussAdd database failure.", e);
        }
    }
    
}
